package com.xms.file.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.file.service.IAttachmentCreaterService;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

/**
 * <p>
 * AttachmentCreaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/27
 */
@RestController
@RequestMapping("/{org}/api/attachment")
public class AttachmentCreaterController extends ApiBaseController {
    private IAttachmentCreaterService _attachmentCreaterService;
    @Autowired
    public void AttachmentCreaterController(IAttachmentCreaterService attachmentCreaterService) {
        _attachmentCreaterService = attachmentCreaterService;
    }

    @ApiOperation("新增附件")
    @PostMapping(value = "create", consumes = FileUploadBase.MULTIPART_FORM_DATA)
    public JsonResultObject create(@RequestPart(value = "file", required = true) MultipartFile[] file,
                                   @RequestParam(value = "entityid", required = true) UUID entityid,
                                   @RequestParam(value = "objectid", required = true) UUID objectid) throws XmsException, IOException {
        if(UUIDUtil.isEmpty(entityid) || UUIDUtil.isEmpty(objectid)){
            return JNotSpecifiedParams("entityid", "objectid");
        }
        if(file == null || file.length == 0){
            return JNotSpecifiedParams("file");
        }
        _attachmentCreaterService.createMany(entityid, objectid, Arrays.asList(file));
        return JOk();
    }
}
